import { MongoClient, Server } from 'mongodb'
import app from '../app'
import UserDao from '../db/userDao';
import PlayerDao from '../db/playerDao';
import PlayerBakDao from '../db/playerBakDao';
import ServerDao from '../db/serverDao';
import GameDao from '../db/gameDao';
import VersionDao from '../db/versionDao';
import ChargeDao from '../db/chargeDao';
import AarenaDao from '../db/arenaDao';
import CsAarenaDao from '../db/csArenaDao';
import MailDao from '../db/mailDao'
import GiftDao from '../db/giftDao'
import GiftListDao from '../db/giftListDao'
import PlayerAndGiftDao from '../db/playerAndGiftDao'
import UserAdminDao from '../db/adminUserDao'
import CountersDao from '../db/counterDao'
import AdminRecordDao from '../db/adminRecordDao'
import CheatDao from 'src/db/cheatDao';
import VersionCheatDao from 'src/db/versionCheatDao';
class MongodbHandle {
    db: any
    dbTest: any
    userDao: UserDao
    playerDao: PlayerDao
    versionCheatDao: VersionCheatDao
    cheatDao: CheatDao
    serverDao: ServerDao
    gameDao: GameDao
    versionDao: VersionDao
    chargeDao: ChargeDao
    userDaoTest: UserDao
    playerDaoTest: PlayerDao
    arenaDao: AarenaDao
    mailDao: MailDao
    giftDao: GiftDao
    giftListDao: GiftListDao
    playerAndGiftDao: PlayerAndGiftDao
    csAarenaDao: CsAarenaDao
    playerBakDao: PlayerBakDao
    playerBakDaoTest: PlayerBakDao
    userAdminDao: UserAdminDao
    arenaDaoTest: AarenaDao
    csAarenaDaoTest: CsAarenaDao
    countersDao :CountersDao
    adminRecordDao: AdminRecordDao
    constructor() {
        this.db = null
        this.dbTest = null
        // this.userDao = null
        // this.playerDao = null
        // this.serverDao = null
        // this.gameDao = null
        // this.chargeDao = null
        // this.userDaoTest = null
        // this.playerDaoTest = null

    }
    init = async (serverName?: string) => {
        console.log(app.config.mongodbUrl + ' ' + app.config.mongodbName + " 数据库开始连接中...");
        let db = await MongoClient.connect(app.config.mongodbUrl, {
            useNewUrlParser: true,
            useUnifiedTopology: true
        })
        this.db = db
        var dbase = db.db(app.config.mongodbName)
        this.userDao = new UserDao(dbase)
        this.playerDao = new PlayerDao(dbase)
        this.playerBakDao = new PlayerBakDao(dbase)
        this.serverDao = new ServerDao(dbase)
        this.versionDao = new VersionDao(dbase)
        this.gameDao = new GameDao(dbase)
        await this.gameDao.init()
        this.chargeDao = new ChargeDao(dbase)
        this.arenaDao = new AarenaDao(dbase)
        this.csAarenaDao = new CsAarenaDao(dbase)
        this.mailDao = new MailDao(dbase)
        this.giftDao = new GiftDao(dbase)
        this.giftListDao = new GiftListDao(dbase)
        this.playerAndGiftDao = new PlayerAndGiftDao(dbase)
        this.userAdminDao = new UserAdminDao(dbase)
        this.adminRecordDao = new AdminRecordDao(dbase)
        this.countersDao = new CountersDao(dbase)
        this.cheatDao = new CheatDao(dbase)
        this.versionCheatDao = new VersionCheatDao(dbase)
        this.countersDao.new("user")
        this.countersDao.new("player")
        console.log(app.config.mongodbUrl + ' ' + app.config.mongodbName + " 数据库连接成功!");
    }

    initTest = async (mongodbUrl, mongodbName) => {
        let db = await MongoClient.connect(mongodbUrl, {
            useNewUrlParser: true
        })
        console.log(mongodbUrl + "数据库连接成功!");
        this.dbTest = db
        var dbase = db.db(mongodbName)
        this.userDaoTest = new UserDao(dbase)
        this.playerDaoTest = new PlayerDao(dbase)
        this.playerBakDaoTest = new PlayerBakDao(dbase)
        this.arenaDaoTest = new AarenaDao(dbase)
        this.csAarenaDaoTest = new CsAarenaDao(dbase)

    }
}

let mongodbHandle = new MongodbHandle()

export default mongodbHandle